package com.ws.shop.interceptor;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ws.shop.action.front.BaseFrontAction;
import com.ws.shop.entity.ArticleCategories;
import com.ws.shop.entity.Category;
import com.ws.shop.entity.Links;
import com.ws.shop.entity.Member;
import com.ws.shop.service.ArticleCategoriesService;
import com.ws.shop.service.CategoryService;
import com.ws.shop.service.LinksService;
import com.ws.shop.util.CommonUtil;
import com.ws.shop.util.SystemConfigUtil;
/**  
 * 拦截器  
 */  
public class FrontRequestInterceptor extends HandlerInterceptorAdapter {
	private static Logger log = Logger.getLogger(FrontRequestInterceptor.class);
	private CategoryService categoryService;
	private ArticleCategoriesService articleCategoriesService;
	private LinksService linksService;
	
    public LinksService getLinksService() {
		return linksService;
	}

	public void setLinksService(LinksService linksService) {
		this.linksService = linksService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	public ArticleCategoriesService getArticleCategoriesService() {
		return articleCategoriesService;
	}

	public void setArticleCategoriesService(
			ArticleCategoriesService articleCategoriesService) {
		this.articleCategoriesService = articleCategoriesService;
	}

	//预处理   
    public boolean preHandle(HttpServletRequest request,   
            HttpServletResponse response, Object handler) throws Exception {
    	Map<String, Object> loginMember = BaseFrontAction.getLoginMember(request.getSession(false));
		StringBuilder sb = new StringBuilder();
		String uri = request.getRequestURI();
		String ip = CommonUtil.getIpAddrByRequest(request);
		sb.append("{").append(ip).append("}{");
		if(loginMember == null){
			sb.append("nologin}{");
		}else{
			sb.append(loginMember.get(Member.LOGIN_MEMBER_NAME_KEY)).append("}{");
		}
		sb.append(request.getMethod()).append(" ").append(uri).append("}{");
		Map parameters = request.getParameterMap();
		Object[] arr = null;
		for (Object key : parameters.keySet()) {
			arr = (Object[])parameters.get(key);
			if(arr != null && arr instanceof String[]){
				sb.append(key).append("=").append(Arrays.toString(arr));
			}
		}
		sb.append("}");
		log.info(sb.toString());
        return true;   
    }   
  
    public void postHandle(HttpServletRequest request,   
          HttpServletResponse response, Object handler,   
          ModelAndView modelAndView) throws Exception {
    	if(modelAndView == null)
    		return ;
    	Map<String, Object> map = modelAndView.getModel();
    	if(map != null){
    		Map<String, Object> m = BaseFrontAction.getLoginMember(request.getSession(false));
    		if(m != null)
    			map.put(BaseFrontAction.LOGIN_MEMBER_SESSION_KEY, m);
    		//商品分类导航
    		List<Category> categoryList = categoryService.findAllCategoryList();
    		map.put("categoryList", categoryList);
    		//系统配置
    		map.put("systemConfig", SystemConfigUtil.getSystemConfig());
    		//底部文章导航
    		List<ArticleCategories> bottomNavigationWithArticle = articleCategoriesService.findBottomNavigationWithArticle(SystemConfigUtil.getSystemConfig().getBottomNavigationCategories());
    		map.put("bottomNavigationWithArticle", bottomNavigationWithArticle);
    		//友情链接
    		List<Links> linksList = linksService.findLinksList(1);
    		map.put("linksList", linksList);
    	}
  }   
}  
